package com.shijie.log.demo;

import com.shijie.log.comparator.BigDecimalFieldComparator;
import com.shijie.log.comparator.FieldComparatorUtil;
import com.shijie.log.formatter.ValueFormatter;
import com.shijie.log.recorder.ChgsLogRecorder;
import com.shijie.log.util.FieldDiffLogUtil;

import java.math.BigDecimal;
import java.time.LocalDateTime;

public class Main {


    public static void main(String[] args) {

        ChgsLogRecorder chgsLogRecorder = new ChgsLogRecorder();

        FieldDiffLogUtil.fieldDiff()
                .addField("user_name", "111", "111", FieldComparatorUtil.DEFAULT_COMPARATOR, ValueFormatter.DEFAULT_FORMATTER)
                .addField("user_email", "111", "111", FieldComparatorUtil.DEFAULT_COMPARATOR, ValueFormatter.DEFAULT_FORMATTER)
                .addField("user_phone", "111", "111", FieldComparatorUtil.DEFAULT_COMPARATOR, ValueFormatter.DEFAULT_FORMATTER)
                .batchRecord(chgsLogRecorder);

        FieldDiffLogUtil.fieldDiff("order_amount",
                        new BigDecimal("222.222"),
                        new BigDecimal("222.233"),
                        new BigDecimalFieldComparator(2),
                        ValueFormatter.DEFAULT_FORMATTER)
                .record(chgsLogRecorder);

        multiFieldExample();
    }

    /**
     * 多字段 + 自定义比较器示例
     */
    private static void multiFieldExample() {
        // 测试数据
        String oldName = "张三";
        String newName = "李四";

        BigDecimal oldPrice = new BigDecimal("10.123");
        BigDecimal newPrice = new BigDecimal("10.126");

        LocalDateTime oldTime = LocalDateTime.now().minusDays(1);
        LocalDateTime newTime = LocalDateTime.now();

        // 构建多字段比较器
        int changeCount = FieldDiffLogUtil.fieldDiff()
                // 字段1：姓名（默认比较器：精确匹配）
                .addField("name", oldName, newName)
                // 字段2：价格（自定义BigDecimal精度：保留2位小数）
                .addField(
                        "price",
                        oldPrice,
                        newPrice,
                        new BigDecimalFieldComparator(2),
                        ValueFormatter.DEFAULT_FORMATTER
                )
                // 字段3：LocalDateTime（日期格式化：yyyy-MM-dd）
                .addField(
                        "createTime",
                        oldTime,
                        newTime,
                        FieldComparatorUtil.DEFAULT_COMPARATOR,
                        ValueFormatter.dateFormatter("yyyy-MM-dd")  // 日期格式化
                )
                .batchRecord(new ChgsLogRecorder());  // 批量记录变更

        System.out.println("多字段变更数量：" + changeCount);  // 此处应为4（所有字段均有变更）
    }


}
